#include <iostream>
#include <memory>
#include <string>

// 抽象工厂模式
//由抽象工厂生产子类工厂
//再由子类工厂模式生产产品


//如下为两个系列的产品
//水果和动物
class Fruit
{
public:
    Fruit() {}
    virtual void show() = 0;
};

class Apple : public Fruit
{
public:
    Apple() {}
    virtual void show()
    {
        std::cout << "我是⼀个苹果" << std::endl;
    }
};

class Banana : public Fruit
{
public:
    Banana() {}
    virtual void show()
    {
        std::cout << "我是⼀个⾹蕉" << std::endl;
    }
};

class Animal
{
public:
    virtual void name()=0;
};

class Lamp:public Animal
{
public:
    void name() override
    {
        std::cout<<"我是一只山羊\n";
    }
};

class Dog:public Animal
{
public:
    void name() override
    {
        std::cout<<"我是一只小狗\n";
    }
};



class Factory
{
public:
    virtual std::shared_ptr<Fruit> getFruit(const std::string& name)=0;
    virtual std::shared_ptr<Animal> getAnimal(const std::string& name)=0;
};

class FruitFactory:public Factory
{
public:
    std::shared_ptr<Fruit> getFruit(const std::string& name)override
    {
        if(name=="苹果")
        {
            return std::make_shared<Apple>();
        }
        else
        {
            return std::make_shared<Banana>();
        }
    }
    std::shared_ptr<Animal> getAnimal(const std::string& name)override
    {
        return std::make_shared<Dog>();
    }
};

class AnimalFactory:public Factory
{
public:
    std::shared_ptr<Fruit> getFruit(const std::string& name)override
    {
        return std::make_shared<Apple>();
    }
    std::shared_ptr<Animal> getAnimal(const std::string& name)override
    {
        if(name=="山羊")
        {
            return std::make_shared<Lamp>();
        }
        else
        {
            return std::make_shared<Dog>();
        }
    }
};

//抽象类工厂
//由这个工厂类来生产子工厂
class Producer
{
public:
    static std::shared_ptr<Factory> create(const std::string& name)
    {
        if(name=="水果")
        {
            return std::make_shared<FruitFactory>();
        }
        else
        {
            return std::make_shared<AnimalFactory>();
        }
    }
};

int main()
{
    //生产一个水果工厂
    std::shared_ptr<Factory> factory=Producer::create("水果");
    //通过工厂 生产不同水果
    std::shared_ptr<Fruit> fruit=factory->getFruit("苹果");
    fruit->show();
    fruit=factory->getFruit("香蕉");
    fruit->show();

    //生产一个动物工厂
    std::shared_ptr<Factory> animal=Producer::create("动物");
    //通过工厂生产不同动物
    std::shared_ptr<Animal> a=animal->getAnimal("山羊");
    a->name();
    a=animal->getAnimal("小狗");
    a->name();

    return 0;
}